package com.ktjy.ktmall.servlet;

import cn.hutool.core.lang.hash.Hash;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ktjy.ktmall.entity.*;
import com.ktjy.ktmall.service.OrderService;
import com.ktjy.ktmall.service.impl.OrderServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// 处理订单相关的请求
@WebServlet(name = "OrderServlet", urlPatterns = "/uni/member/order")
public class OrderServlet extends HttpServlet {

    private OrderService orderService = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        orderService = new OrderServiceImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、创建out对象
        PrintWriter out = response.getWriter();
        // 2、获取从小程序端请求过来的参数
        String params = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
        // 3、将请求参数字符串转换成JSONObject对象，之后从JSONObject对象中取出值
        JSONObject entries = JSONUtil.parseObj(params);
        String opr = entries.get("opr", String.class);
        AjaxResult result = null;
        if ("addOrder".equals(opr)) {
            // 下单
            // 3.1 获取从小程序端请求过来的订单对象
            Order order = entries.get("order", Order.class);
            int id = orderService.saveOrder(order);
            Map<String, Integer> map = new HashMap<>();
            map.put("id", id);
            if(id > 0) {
                result = AjaxResult.success("下单成功!", map);
            } else {
                result = AjaxResult.error("下单失败!");
            }
        } else if ("findById".equals(opr)) {
            // 通过订单id查询订单详情信息
            // 3.1 获取从小程序端请求过来的订单id
            Integer orderId = entries.get("orderId", Integer.class);
            Order order = orderService.findById(orderId);
            result = AjaxResult.success("订单详情查询成功!", order);
        } else if ("pageOrderList".equals(opr)) {
            // 分页查询订单列表
            // 1、接收从小程序传递过来的分页查询
            OrderParam orderParam = entries.get("orderParam", OrderParam.class);
            // 2、调用serivce中查询总记录数的方法
            int count = orderService.getCount(orderParam.getUserId(), orderParam.getOrderState());
            // 5、调用service中查询订单的分页列表数据的方法
            List<Order> list = orderService.getOrderListPage(orderParam.getCurrPageNo(), orderParam.getPageSize(), orderParam.getUserId(), orderParam.getOrderState());
            // 6、封装Page对象
            Page page = new Page();
            page.setCurrPageNo(orderParam.getCurrPageNo());
            page.setPageSize(orderParam.getPageSize());
            page.setTotalCount(count);
            page.setList(list); // 将查询到的商品分页列表数据进行赋值

            result = AjaxResult.success("订单列表加载成功!", page);
        }
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
